﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

public class LoadFromAB : MonoBehaviour {
    string serverPath;

    //不同平台下StreamingAssets的路径是不同的，这里需要注意一下。
    public static readonly string localPath =
#if UNITY_ANDROID
		"jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
		Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
 "file://" + Application.dataPath + "/AssetBundlePackage/";
#else
        string.Empty;
#endif

    void Start()
    {
        serverPath = ServerConfig.ServerConfig.GetTestServer();
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(100, 100, 90, 50), "Sphere"))
        {
            StartCoroutine(LoadMainGameObject(localPath+ "Sphere.assetbundle"));
        }
        if (GUI.Button(new Rect(100, 200, 90, 50), "Cube"))
        {
            StartCoroutine(LoadMainGameObject(serverPath + "Cube.assetbundle"));
        }
        if (GUI.Button(new Rect(100, 300, 100, 100), "2D UI"))
        {
            StartCoroutine(LoadMainGameObject(serverPath + "2D UI.assetbundle"));
        }
        if (GUI.Button(new Rect(100, 400, 100, 100), "12345"))
        {
            StartCoroutine(LoadMainGameObject(serverPath + "12345.assetbundle"));
        }
    }

    //读取一个资源
    private IEnumerator LoadMainGameObject(string path)
    {
        WWW bundle = new WWW(path);
        yield return bundle;
        Object obj = Instantiate(bundle.assetBundle.mainAsset);
        obj.name = obj.name.Substring(0, obj.name.Length - 7); //去掉名字后面的(clone)
        yield return obj;
        bundle.assetBundle.Unload(false);
    }


}
